package org.wsjy.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.wsjy.model.TCulture;
import org.wsjy.model.TService;
import org.wsjy.service.CultureService;
import org.wsjy.service.ServiceService;
import org.wsjy.util.DataEncapsulation;

/**
 * 关于我们的信息
 *
 * @Title:
 * @Company:万世极缘工作室
 * @Description:
 * @Author:万世极缘
 * @Since:2016年2月16日
 * @Copyright:Copyright (c) 2016
 * @ModifyDate:
 * @Version:1.1.0
 */
@Controller
@RequestMapping("/culture")
public class CultureController {
    @Autowired
    private CultureService cultureService;
    @Autowired
    private ServiceService serviceService;
    
    @RequestMapping("about.asp")
    public String about(ModelMap model) {
        // 因为culture在系统启动的时候已经存放进上下文中了，所以直接从上下文中获取即可
        List<TService> serviceList = serviceService.findAll();
        model.addAttribute("serviceList", serviceList);
        return "culture/about";
    }

    @RequestMapping("update.asp")
    public String add() {
        // 进入编辑页面，如果公司暂未存在公司的信息，那么将是新增
        return "culture/update";
    }

    @RequestMapping("save.asp")
    public String save(HttpServletRequest request) {
        // 将提交的数据保存到数据库中
        try {
            TCulture culture = DataEncapsulation.request2Bean(request, TCulture.class);
            // 保存进数据库
            cultureService.save(culture);
            // 并修改上下文中的culture
            request.getSession().getServletContext().setAttribute("culture", culture);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "culture/save";
    }
}
